package com.dou.you.che.service.impl.sys;

import com.dou.you.che.service.sys.SysUserRoleServiceI;
import com.dou.you.che.core.mapper.sys.SysUserRoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户-角色
 * @Author 黄国刚【1058118328@qq.com】
 */
@Service
@Transactional(readOnly = true)
public class SysUserRoleService implements SysUserRoleServiceI {

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Transactional(readOnly = false)
    @Override
    public void saveOrUpdate(String userId, List<String> roleIdList) {
        if (roleIdList.size() == 0) {
            return;
        }

        //先删除用户与角色关系
        sysUserRoleDao.delete(userId);

        //保存用户与角色关系
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("roleIdList", roleIdList);
        sysUserRoleDao.save(map);
    }

    @Override
    public List<String> queryRoleIdList(String userId) {
        return sysUserRoleDao.queryRoleIdList(userId);
    }

    @Transactional(readOnly = false)
    @Override
    public void delete(String userId) {
        sysUserRoleDao.delete(userId);
    }
}
